package co.com.travelsoft.dao.imp.maestras;


import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import co.com.travelsoft.dao.interfaces.maestras.VendedorDAOInterface;
import co.com.travelsoft.entities.maestras.Vendedor;
import co.com.travelsoft.util.exceptions.TsDAOException;
import co.com.travelsoft.util.exceptions.TsObjetNotFoundException;


/**
 * Dao {@link Vendedor}
 * @author Juan Carlos Cardona
 * @since  23/04/2014
 * @version 1.0
 */
public class VendedorDAO extends HibernateDaoSupport implements VendedorDAOInterface {
	

	@Override
	public Vendedor findByCodigo(String codigo) throws TsObjetNotFoundException {
	    String hql = "Select p from Vendedor p where codigo=:codigo";
	    Query query = getSession().createQuery(hql);
	    query.setParameter("codigo", codigo);
	    Vendedor result = (Vendedor) query.uniqueResult();
	    if(result==null){
		throw new TsObjetNotFoundException("El Vendedor con codigo:" + codigo + " no existe.");
	    }
	    return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Vendedor> getVendedorsList() throws TsDAOException {
	    String hql = "Select c from Vendedor c";
	    Query query = getSession().createQuery(hql);
	    List<Vendedor> listReturn = query.list();
	    return  listReturn;
	}
}
